$(document).ready(function () {
    // 下载拦截，下载加密以及下载数count
    $("#fileDownloadBtn").click(function () {

        var fileDownloadBtn = document.getElementById("fileDownloadBtn");

        var fileInfoId = fileDownloadBtn.getElementsByClassName("fileInfoIdClass")[0].innerText;

        $.getJSON("/api/countDownload/" + fileInfoId, function (res) {
            if(res.code == -1) {
                toastr.warning(res.msg);

            } else {
                var href = $("#fileURIInfo").attr("href");
                // 确认href的前几位是否是encode：是，需要ajax解密；不是，不需要解密了；
                // console.log(href);
                // var str = href.substring(6, href.length-1);
                // console.log(str);

                if (href.substring(0, 6) != "encode") {
                    document.getElementById("fileURIInfo").click();
                } else {
                    var str = href.substring(6, href.length);
                    // console.log(str);

                    $.ajax({
                        type: "POST",
                        url: '/api/uri/encode',
                        data: "uri=" + str,
                        success:
                            function (res) {
                                if (res.code == 0) {
                                    var uri = res.data;

                                    $("#fileURIInfo").attr("href", uri);
                                    document.getElementById("fileURIInfo").click();

                                    var times = $("#fileDownloadTimesInfo").text();

                                    var timesNew = parseInt(times) + 1;
                                    $("#fileDownloadTimesInfo").text(timesNew);
                                } else {
                                    toastr.error(res.msg);
                                }
                            },
                        error:
                            function (res) {
                                toastr.error('系统接口异常，请稍后');
                            }
                    });
                }

            }


        });
    });

    // 点赞数count，点赞拦截
    $("#fileStarBtn").click(function () {

        var fileStarBtn = document.getElementById("fileStarBtn");

        var fileInfoId = fileStarBtn.getElementsByClassName("fileInfoIdClass")[0].innerText;

        $.getJSON("/api/countStar/" + fileInfoId, function (res) {
            if (res.code == 0) {
                var times = $("#fileStarInfo").text();

                var timesNew = parseInt(times) + 1;
                $("#fileStarInfo").text(timesNew);

                toastr.success(res.msg);

            } else {
                toastr.warning(res.msg);

            }

        });
    });
});